package org.zyx.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zyx.item.entity.Brand;
import org.zyx.item.entity.Category;
import org.zyx.item.mapper.BrandMapper;
import org.zyx.yiyou.common.entity.PageResult;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BrandService {

    @Resource
    private BrandMapper brandMapper;

    /**
     * 根据查询条件分页并排序查询品牌信息
     *
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<Brand> findBrandByPage(String key, int page, int rows, String sortBy, Boolean desc) {
        //初始化example对象
        Example example = new Example(Brand.class);//例子,用于拼接sql语句
        Example.Criteria criteria = example.createCriteria();//条件
        //key:根据name模糊查询,或者根据首字母查询
        if (StringUtils.isNotBlank(key)) {
            criteria.andLike("name", "%" + key + "%").orEqualTo("letter", key);
        }
        //添加分页条件
        PageHelper.startPage(page, rows);//pageHelper分页工具
        //添加排序条件
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc"));
        }
        //执行查询
        List<Brand> brands = brandMapper.selectByExample(example);

        //包装成PageInfo对象
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增品牌
     *
     * @param brand 品牌对象
     * @param cids  分类数组
     */
    @Transactional  //添加事务,异常直接回滚,判断可以直接省略
    public void savaBrand(Brand brand, List<Long> cids) {
        //1.新增Brand表
        boolean flag = brandMapper.insertSelective(brand) == 1;//返回id到实体类中
        //2.新增categroy_brand中间表数据
        if (flag) {
            cids.forEach(cid -> {  //遍历cids数组,每一个cid添加一条数据
                this.brandMapper.insertCategoryAndBrand(cid, brand.getId());
            });
        }
    }

    /**
     * 更新品牌信息
     * @param brand
     * @param cids
     */
    @Transactional
    public void updBrand(Brand brand, List<Long> cids) {
        //1.更新Brand表
        brandMapper.updateByPrimaryKeySelective(brand);//返回id到实体类中
        //2.更新categroy_brand中间表数据,先删除,后添加
        brandMapper.deleteByBrandIdInCategoryBrand(brand.getId());
        //添加新的分类
        cids.forEach(cid -> {  //遍历cids数组,每一个cid添加一条数据
            this.brandMapper.insertCategoryAndBrand(cid, brand.getId());
        });
    }

    /**
     * 通过品牌id查找分类
     *
     * @param bid
     * @return
     */
    public List<Category> findCategorysByBid(Long bid) {
        List<Category> categorys = brandMapper.findCidsByBid(bid);
        return categorys;
    }

    /**
     * 通过id删除品牌
     * @param bid
     */
    public void deleteByBid(Long bid) {
        brandMapper.deleteByBrandIdInCategoryBrand(bid);//删除分类
        brandMapper.deleteByPrimaryKey(bid);//删除品牌

    }

    /**
     * 根据分类查找品牌
     * @param cid
     * @return
     */
    public List<Brand> findBrandsByCid(Long cid) {
        return brandMapper.findBrandsByCid(cid);
    }

    /**
     * 通过id查找分类
     * @param id
     * @return
     */
    public Brand findBrandById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }
}
